﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WCS.Utils;
using WCS.Helpers;
using WCS.Entity;
using WcsTask;
using WcsTask.DevCommTaks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using SqlSugar;
using Sunny.UI.Win32;
using Newtonsoft.Json.Linq;
using System.Web.UI;
using System.Reflection;

namespace WCS.Controls.系统查询
{
    public partial class ChangeAllowInOut : UIForm
    {
        public ChangeAllowInOut()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper SqlSugarDapper = SqlSugarDapper.GetDbContext();


        private void AddTask_Load(object sender, EventArgs e)
        {
            var ss = SqlSugarDapper.DB.Queryable<Warehouse>().OrderBy("WarehouseCode asc").OrderBy("layer asc").ToList();

            for (int i = 0; i < ss.Count; i++)
            {
                if (ss[i].IsIN == 1)
                {
                    foreach (System.Windows.Forms.Control ctrl in this.Controls)
                    {
                        if (ctrl.GetType().Name.Contains("UISwitch"))
                        {
                            UISwitch uISwitch = (UISwitch)ctrl;
                            if (uISwitch.Name == "uiSwitchIn" + $"{i + 1}")
                            {
                                uISwitch.Active = true;
                                break;
                            }
                        }
                    }
                }
                if (ss[i].IsOUT == 1)
                {
                    foreach (System.Windows.Forms.Control ctrl in this.Controls)
                    {
                        if (ctrl.GetType().Name.Contains("UISwitch"))
                        {
                            UISwitch uISwitch = (UISwitch)ctrl;
                            if (uISwitch.Name == "uiSwitchOut" + $"{i + 1}")
                            {
                                uISwitch.Active = true;
                                break;
                            }
                        }
                    }
                }
            }

        }

        public void Change(string control, string controlName, string layer, string warehourseCode, string type)
        {
            foreach (System.Windows.Forms.Control ctrl in this.Controls)
            {
                if (ctrl.GetType().Name.Contains(control))
                {
                    if (control == "UISwitch")
                    {
                        UISwitch uISwitch = (UISwitch)ctrl;

                        if (uISwitch.Name == controlName)
                        {
                            if (warehourseCode == "9001")
                            {
                                if (type == "in")
                                {
                                    if (uISwitch.Active == true)
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsIN == 1)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    else
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsIN == 0)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    ChangeMaterialType._ChangeMaterialType.Re();
                                    return;
                                }
                                else
                                {
                                    if (uISwitch.Active == true)
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsOUT == 1)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    else
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsOUT == 0)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    ChangeMaterialType._ChangeMaterialType.Re();
                                    return;
                                }

                            }
                            if (warehourseCode == "9002")
                            {
                                if (type == "in")
                                {
                                    if (uISwitch.Active == true)
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsIN == 1)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    else
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsIN == 0)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    ChangeMaterialType._ChangeMaterialType.Re();
                                    return;
                                }
                                else
                                {
                                    if (uISwitch.Active == true)
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsOUT == 1)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    else
                                    {
                                        SqlSugarDapper.DB.Updateable<Warehouse>()
                                        .SetColumns(w => w.IsOUT == 0)
                                        .Where(w => w.layer == layer && w.WarehouseCode == warehourseCode)
                                        .ExecuteCommand();
                                    }
                                    ChangeMaterialType._ChangeMaterialType.Re();
                                    return;
                                }
                            }
                        }
                    }
                }
            }


        }

        #region C区 出 开关
        private void uiSwitchOut1_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{1}", "1", "9001", "out");
        }

        private void uiSwitchOut2_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{2}", "2", "9001", "out");
        }

        private void uiSwitchOut3_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{3}", "3", "9001", "out");
        }

        private void uiSwitchOut4_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{4}", "4", "9001", "out");
        }
        private void uiSwitchOut5_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{5}", "5", "9001", "out");
        }

        private void uiSwitchOut6_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{6}", "6", "9001", "out");
        }

        private void uiSwitchOut7_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{7}", "7", "9001", "out");
        }

        private void uiSwitchOut8_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{8}", "8", "9001", "out");
        }

        private void uiSwitchOut9_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{9}", "9", "9001", "out");
        }

        private void uiSwitchOut10_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{10}", "10", "9001", "out");
        }
        #endregion

        #region C区 入 开关
        private void uiSwitchIn1_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{1}", "1", "9001", "in");
        }

        private void uiSwitchIn2_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{2}", "2", "9001", "in");
        }

        private void uiSwitchIn3_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{3}", "3", "9001", "in");
        }

        private void uiSwitchIn4_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{4}", "4", "9001", "in");
        }

        private void uiSwitchIn5_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{5}", "5", "9001", "in");
        }

        private void uiSwitchIn6_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{6}", "6", "9001", "in");
        }

        private void uiSwitchIn7_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{7}", "7", "9001", "in");
        }

        private void uiSwitchIn8_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{8}", "8", "9001", "in");
        }

        private void uiSwitchIn9_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{9}", "9", "9001", "in");
        }

        private void uiSwitchIn10_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{10}", "10", "9001", "in");
        }
        #endregion

        #region D区 入 开关
        private void uiSwitchIn11_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{11}", "1", "9002", "in");
        }

        private void uiSwitchIn12_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{12}", "2", "9002", "in");
        }

        private void uiSwitchIn13_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{13}", "3", "9002", "in");
        }

        private void uiSwitchIn14_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{14}", "4", "9002", "in");
        }

        private void uiSwitchIn15_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{15}", "5", "9002", "in");
        }

        private void uiSwitchIn16_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{16}", "6", "9002", "in");
        }

        private void uiSwitchIn17_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{17}", "7", "9002", "in");
        }

        private void uiSwitchIn18_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{18}", "8", "9002", "in");
        }

        private void uiSwitchIn19_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{19}", "9", "9002", "in");
        }

        private void uiSwitchIn20_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchIn{20}", "10", "9002", "in");
        }
        #endregion

        #region D区 出 开关
        private void uiSwitchOut11_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{11}", "1", "9002", "out");
        }

        private void uiSwitchOut12_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{12}", "2", "9002", "out");
        }

        private void uiSwitchOut13_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{13}", "3", "9002", "out");
        }

        private void uiSwitchOut14_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{14}", "4", "9002", "out");
        }

        private void uiSwitchOut15_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{15}", "5", "9002", "out");
        }

        private void uiSwitchOut16_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{16}", "6", "9002", "out");
        }

        private void uiSwitchOut17_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{17}", "7", "9002", "out");
        }

        private void uiSwitchOut18_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{18}", "8", "9002", "out");
        }

        private void uiSwitchOut19_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{19}", "9", "9002", "out");
        }

        private void uiSwitchOut20_ValueChanged(object sender, bool value)
        {
            Change("UISwitch", $"uiSwitchOut{20}", "10", "9002", "out");
        }
        #endregion
    }
}
